home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / internet / html-related / hsc / source / hsclib / entity.c < prev    next >
C/C++ Source or Header  |  1996-05-17  |  4KB  |  231 lines

  1. /*
  2. ** entity.c
  3. **
  4. ** entity structure, variables and functions ( "&xx;")
  5. **
  6. ** Copyright (C) 1995,96  Thomas Aglassinger
  7. **
  8. ** This program is free software; you can redistribute it and/or modify
  9. ** it under the terms of the GNU General Public License as published by
  10. ** the Free Software Foundation; either version 2 of the License, or
  11. ** (at your option) any later version.
  12. **
  13. ** This program is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ** GNU General Public License for more details.
  17. **
  18. ** You should have received a copy of the GNU General Public License
  19. ** along with this program; if not, write to the Free Software
  20. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. **
  22. ** updated: 17-Mar-1996
  23. ** created:  8-Sep-1995
  24. **
  25. */
  26.  
  27. #define NOEXTERN_HSCLIB_ENTITY_H
  28. #include "hsclib/inc_base.h"
  29. #include "hsclib/entity.h"
  30.  
  31. /*
  32. **-------------------------------------
  33. ** constructor/destructor
  34. **-------------------------------------
  35. */
  36.  
  37. /*
  38. ** new_hscent
  39. **
  40. ** alloc & init a new hscentity
  41. */
  42. HSCENT *new_hscent( STRPTR newid )
  43. {
  44.  
  45.     HSCENT *newent = (HSCENT*) umalloc( sizeof(HSCENT) );
  46.  
  47. #if DEBUG_ENTITY
  48.     fprintf( stderr, DHL "   new_enty %s\n", newid );
  49. #endif
  50.  
  51.     if (newent) {
  52.  
  53.         /* init new entity item */
  54.         newent->name    = strclone(newid);       /* set id */
  55.         newent->replace = NULL;
  56.         newent->numeric = 0L;
  57.     }
  58.  
  59.     return (newent);
  60.  
  61. }
  62.  
  63. /*
  64. ** del_entity
  65. */
  66. VOID del_entity( APTR data )
  67. {
  68.     HSCENT *ent = (HSCENT *)data;
  69.  
  70. #if DEBUG_ENTITY
  71.     fprintf( stderr, DHL "   del_enty %s\n", ent->name );
  72. #endif
  73.  
  74.     ufreestr( ent->name );
  75.     ufreestr( ent->replace );
  76.     ent->numeric = 0;
  77.     ufree( ent );
  78.  
  79. }
  80.  
  81. /*
  82. ** cpy_hscent
  83. **
  84. ** create a copy of an already existing entity
  85. */
  86. HSCENT *cpy_hscent( HSCENT *oldent )
  87. {
  88.     HSCENT *newent = new_hscent( oldent->name );
  89.  
  90.     if ( newent ) {
  91.  
  92.         newent->replace = strclone( oldent->replace );
  93.         newent->numeric = oldent->numeric;
  94.  
  95.     }
  96.  
  97.     return( newent );
  98. }
  99.  
  100.  
  101. /*
  102. **---------------------------
  103. ** find entity string
  104. **---------------------------
  105. */
  106.  
  107. /*
  108. ** cmp_strent
  109. **
  110. ** compares a entity-string with the name
  111. ** of a HSCENT-entry
  112. */
  113. int cmp_strent( APTR cmpstr, APTR entdata )
  114. {
  115.     STRPTR entstr = NULL;
  116.  
  117.     if ( entdata )
  118.         entstr = ((HSCENT*)entdata)->name;
  119.  
  120.     if (entstr)
  121.         if ( !strcmp( cmpstr, entstr ) )
  122.             return (-1);
  123.         else
  124.             return (0);
  125.     else
  126.         return (0);
  127. }
  128.  
  129. /*
  130. ** cmp_nument
  131. **
  132. ** compares a entity-string with the numeric
  133. ** data of a HSCENT-entry
  134. */
  135. int cmp_nument( APTR cmpstr, APTR entdata )
  136. {
  137.     LONG num = -1;
  138.     LONG cmpnum = (LONG) cmpstr;
  139.  
  140.     if ( entdata )
  141.         num = ((HSCENT*)entdata)->numeric;
  142.  
  143.     return( ( num != -1 ) && ( num == cmpnum  ) );
  144. }
  145.  
  146. /*
  147. ** cmp_rplcent
  148. **
  149. ** compares a entity-string with the replace-item
  150. ** of a HSCENT-entry
  151. */
  152. int cmp_rplcent( APTR cmpstr, APTR entdata )
  153. {
  154.     STRPTR entstr = NULL;
  155.  
  156.     if ( entdata )
  157.         entstr = ((HSCENT*)entdata)->replace;
  158.  
  159.     if (entstr)
  160.         if ( !strcmp( cmpstr, entstr ) )
  161.             return (-1);
  162.         else
  163.             return (0);
  164.     else
  165.         return (0);
  166. }
  167.  
  168. /*
  169. **-------------------------------------
  170. ** append entity functions
  171. **-------------------------------------
  172. */
  173.  
  174. /*
  175. ** app_entnode
  176. **
  177. ** create a new entity and append it to entity-list
  178. **
  179. ** params: entid..name of the new entity (eg "uuml")
  180. ** result: ptr to the new entity or NULL if no mem
  181. */
  182. HSCENT *app_entnode( DLLIST *entlist, STRPTR entid )
  183. {
  184.     HSCENT *newent;
  185.  
  186.     newent = new_hscent( entid );
  187.     if ( newent ) {
  188.  
  189.         if (app_dlnode( entlist, newent ) == NULL ) {
  190.  
  191.             del_entity( (APTR) newent );
  192.             newent = NULL;
  193.  
  194.         }
  195.     }
  196.  
  197.     return (newent);
  198. }
  199.  
  200.  
  201. /*
  202. ** add_ent
  203. */
  204. BOOL add_ent( DLLIST *entlist, STRPTR entid, STRPTR entreplace, LONG num )
  205. {
  206.     HSCENT *newent = app_entnode( entlist, entid );
  207.  
  208.     if ( entreplace )
  209.         newent->replace = strclone( entreplace );
  210.  
  211.     newent->numeric = num;
  212.  
  213. #if 0
  214.     DDE(
  215.         {
  216.  
  217.             STRPTR rplc = entreplace;
  218.             if ( !rplc )
  219.                 rplc = "(empty)";
  220.             fprintf( stderr, DHL "defent: \"%s\" \"%s\" %ld\n",
  221.                              entid, rplc, num );
  222.  
  223.         }
  224.     );
  225. #endif
  226.  
  227.     return (TRUE);
  228.  
  229. }
  230.  
  231.